/**
 * 影像模型
 */
import LayerModel from "../LayerModel";
import store from '../../store/store';

export default class TerrainLayerModel extends LayerModel {

  constructor(layerInfo) {
    super();
    this.layerInfo = layerInfo;

  }

  // 添加图层
  async addLayer() {

    let url = this.layerInfo.url;

    // 判断服务是否已经加载
    if (store.state.viewerLayers[url]) {
      console.error(`TerrainLayerModel添加地形错误，原因：该地形已经存在。url = ${url}。`);
    }

    let terrainProvider;
    try {
      if (this.layerInfo.subdomain) { // 通过子域加载

        terrainProvider = new Cesium.CesiumTerrainProvider({
          url: this.layerInfo.subdomain.subdomainConfig.url,
          subdomains: this.layerInfo.subdomain.subdomainConfig.subdomains,
          // requestWaterMask: false,
          // requestVertexNormals: false,
          isSct: true
        });
      } else if (url.lastIndexOf("/datas/")) {
        terrainProvider = new Cesium.CesiumTerrainProvider({
          url: url, // 只加载第一个地形图层，因为Cesium的地形数据只能设置一层
          requestWaterMask: false,
          requestVertexNormals: false,
          isSct: true
        });
      } else {

        let terrainLayers = await axios.get(`${url}/datas.json`); // 地形图层列表

        terrainProvider = new Cesium.CesiumTerrainProvider({
          url: terrainLayers[0].path, // 只加载第一个地形图层，因为Cesium的地形数据只能设置一层
          requestWaterMask: false,
          requestVertexNormals: false,
          isSct: true
        });
      }
    } catch (errInfo) {
      console.error(`TerrainLayerModel添加图层错误，创建地形提供者错误，请检测配置。原因：${errInfo}`);
      return;
    }

    // 图层加载完成
    terrainProvider.readyPromise.then(() => {

      viewer.terrainProvider = terrainProvider;

      // 保存当前地形对象到模型
      this.terrainProvider = terrainProvider;

      // 将对象添加到图层列表中
      store.state.viewerLayers[url] = this;

    }).otherwise(errInfo => {
      console.error(`TerrainLayerModel添加图层错误，url = ${url}。原因：${errInfo}。`);
    });
  }

  // 删除图层
  removeLayer() {

    viewer.terrainProvider = new Cesium.EllipsoidTerrainProvider();

    let url = this.layerInfo.url;
    if (store.state.viewerLayers[url]) {
      store.state.viewerLayers[url] = undefined;
      delete store.state.viewerLayers[url];
    }

    return true;
  }

  // 飞行
  flyTo() {

    let isFly = super.flyTo(); // 按配置的坐标飞行

    // 没有在配置文件中定义坐标位置坐标
    if (!isFly) {

      let layer = this.terrainProvider;
      let west = Cesium.Math.toRadians(layer._bounds.west);
      let south = Cesium.Math.toRadians(layer._bounds.south);
      let east = Cesium.Math.toRadians(layer._bounds.east);
      let north = Cesium.Math.toRadians(layer._bounds.north);
      let rectangle = new Cesium.Rectangle(west, south, east, north);
      let camera = scene.camera;
      camera.flyTo({
        destination: rectangle
      });
    }
  }

  // 设置图层的可见性
  setVisible(isVisible) {

    if (!this.nowTerrin) {

      this.nowTerrin = [];
      this.nowTerrin[0] = viewer.terrainProvider;
      this.nowTerrin[1] = new Cesium.EllipsoidTerrainProvider();
    }

    if (isVisible) {
      viewer.terrainProvider = this.nowTerrin[0];
    } else {
      viewer.terrainProvider = this.nowTerrin[1];
    }
  }
}

export {
  TerrainLayerModel
}
